(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.txnNoteCreateController', controller);
	controller.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig',
			'authService' ];
	function controller($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService) {
		var ctrl = this, params = $scope.$stateParams.params, log = $scope.loggerFactory.getLogger("txnNotePopup"), dialogParams;
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		dialogParams = dialogsModule.params;
		ctrl.formName = 'txnNotePopupForm';
		ctrl.ok = function() {
			if($scope.txnNotePopupForm) {
				$scope.txnNotePopupForm.$setSubmitted();
				if(!$scope.txnNotePopupForm.$valid) {
					dialogsModule.showErrorMessages($scope.txnNotePopupForm);
					return;
				}
			}
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.txnNote) !== false) {
				$uibModalInstance.close();
			}
		};
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.init = function() {
			ctrl.list();
			ctrl.create();
		};
		ctrl.list = function() {
			ctrl.searchCriteria = {};
			ctrl.searchCriteria.list = [];
			angular.forEach(dialogParams, function(value, key) {
				ctrl.searchCriteria[key] = value;
			});
			//clear
			ctrl.searchCriteria['dataStatus'] = '';
			authService.list(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
			}, angular.noop);
		};
		ctrl.create = function() {
			ctrl.txnNote = {};
			authService.create(params).then(function(response) {
				ctrl.txnNote = response.data || params;
				ctrl.txnNote.txnKey = dialogParams.dataKey;
				ctrl.txnNote.txnType = dialogParams.dataType;
				ctrl.txnNote.noteType = dialogParams.dataStatus;
				ctrl.txnNote.serviceCode = dialogParams.serviceCode;
				ctrl.txnNote.taskId = dialogParams.taskId;
			});
		};
	}
})(angular);